﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using DBLibrary;

namespace TransfertImageService
{
    public class TransfertImage : ITransfertImage
    {
        private Customer customer = new Customer();

        /// <summary>
        /// Upload an image into the DataBase
        /// </summary>
        /// <param name="data">Request containing image's info and data</param>
        public void uploadImage(ImageUploadRequest data)
        {
            // Build an array correpsonding to the image
            //
            byte[] imageByte = null;
            MemoryStream imageMemoryStream = new MemoryStream();
            data.ImageData.CopyTo(imageMemoryStream);
            imageByte = imageMemoryStream.ToArray();

            // Save the image
            //
            data.ImageInfo.ID = customer.addImage(imageByte, data.ImageInfo.IDAlbum);
            imageMemoryStream.Close();
            data.ImageData.Close();
        }

        /// <summary>
        /// Transfert an image to the customer
        /// </summary>
        /// <param name="data">Request containing image's infos</param>
        /// <returns>Returns the image corresponding to the ID</returns>
        public ImageDownloadResponse downloadImage(ImageDownloadRequest data)
        {
            // Get the image in DataBase and transfert it to the customer
            //
            byte[] image = customer.getImage(data.ImageInfo.ID);
            MemoryStream imageMemoryStream = new MemoryStream(image);
            ImageDownloadResponse response = new ImageDownloadResponse();
            response.ImageData = imageMemoryStream;
            return response;
        }
    }
}
